package com.spider.gp.processor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.spider.gp.constant.UrlConstant;
import com.spider.gp.dao.*;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * description:
 *
 * @author clare
 * @date 2022/05/20 08:56
 **/
@Component
public class LianbanProcessor implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Autowired
    private LianBanDao lianBanDao;
    @Autowired
    private ShouBanDao shouBanDao;
    @Override
    public void process(Page page) {
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        String reportTime = formatter.format(new Date());
//        String reportTime = "2022-09-02";
        String s = page.getUrl().get();
        String reportTime = s.substring(s.length() - 8);

        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            Date date = formatter.parse(reportTime);
            SimpleDateFormat formatter01 = new SimpleDateFormat("yyyy-MM-dd");
            reportTime = formatter01.format(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        LambdaQueryWrapper<LianBan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LianBan::getReportTime, reportTime);
        lianBanDao.delete(wrapper);

        LambdaQueryWrapper<ShouBan> wrapperS = new LambdaQueryWrapper<>();
        wrapperS.eq(ShouBan::getReportTime, reportTime);
        shouBanDao.delete(wrapperS);

        List<String> all = page.getJson().jsonPath("$.data.pool").all();
        for (String item : all) {
            HashMap hashMap = JSON.parseObject(item, HashMap.class);
            String lbc = hashMap.get("lbc").toString();//连板
            hashMap.get("amount");//成交额
            String code = hashMap.get("c").toString();//code
            String name = hashMap.get("n").toString();//股票名称
            String hs = new BigDecimal(hashMap.get("hs").toString()).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString();//换手
            String zdp =  new BigDecimal(hashMap.get("zdp").toString()).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString();//换手 hashMap.get("zdp").toString();//涨跌幅
            String hybk = hashMap.get("hybk").toString();//行业板块
            String m = hashMap.get("m").toString();//0 深圳 1：上证
            String p = new BigDecimal(hashMap.get("p").toString()).divide(new BigDecimal(1000)).toString(); //最新价hashMap.get("p").toString();
            String ltsz = new BigDecimal(hashMap.get("ltsz").toString()).divide(new BigDecimal(100000000)).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString() + "亿"; //流通市值 hashMap.get("ltsz").toString();
            String zbc = hashMap.get("zbc").toString();//炸板次数
            String fund = new BigDecimal(hashMap.get("fund").toString()).divide(new BigDecimal(10000)).setScale(2, BigDecimal.ROUND_HALF_UP).toString() + "万";//封板资金
            String fbt = hashMap.get("fbt").toString();//首次封板时间
            String lbt = hashMap.get("lbt").toString();//最后封板时间
            String tshare = new BigDecimal(hashMap.get("tshare").toString()).divide(new BigDecimal(100000000)).setScale(2 ,BigDecimal.ROUND_HALF_UP).toString() + "亿";//总市值 hashMap.get("tshare").toString();
            JSONObject parseObject = JSONArray.parseObject(hashMap.get("zttj").toString());//几天几板
            StringBuffer jtjb = new StringBuffer();
            jtjb.append(parseObject.get("days")).append("天").append(parseObject.get("ct")).append("板");

            if (Integer.valueOf(lbc) < 2){
                ShouBan ban = ShouBan.builder().reportTime(reportTime).code(code).name(name).hs(hs).zdf(zdp).hybk(hybk).lbc(lbc)
                        .m(m).price(p).ltsz(ltsz).zbc(zbc).fund(fund).fbt(fbt).lbt(lbt).tshare(tshare).zttj(jtjb.toString()).build();
                shouBanDao.insert(ban);
            }else {
                LianBan ban = LianBan.builder().reportTime(reportTime).code(code).name(name).hs(hs).zdf(zdp).hybk(hybk).lbc(lbc)
                        .m(m).price(p).ltsz(ltsz).zbc(zbc).fund(fund).fbt(fbt).lbt(lbt).tshare(tshare).zttj(jtjb.toString()).build();
                lianBanDao.insert(ban);
            }

        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}








